package com.ihr360.payroll.controller.spreadsheet;

import com.google.common.collect.Lists;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.excel.entity.CorrectSpreadsheetRowEntity;
import com.ihr360.excel.vo.ImportHeaderGroup;
import com.ihr360.excel.vo.ImportHeaderItem;
import com.ihr360.excel.vo.ImportHeaderResult;
import com.ihr360.excel.vo.ImportRequest;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Api(value = "测试导入的接口")
@RestController
@RequestMapping("/spreadSheet")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.STAFF_PAY_ROLLINFO_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.STAFF_PAY_ROLLINFO_NAME, isPublic = true)
public class SpreadSheetDemoController {

    @PostMapping(value = "/import")
    @FunctionDefinition(functionName = "通用导入demo", functionCode = ModuleCodeStore.Function.IMPORT, isPublic = true)
    public ResultInfo importSpreadSheet(@RequestBody ImportRequest importRequest) {
        System.out.println("成功导入：" + importRequest.getCorrectDatas().size() + "条数据");
        System.out.println("需要导入的表头有：" + importRequest.getImportHeaders());

        List<CorrectSpreadsheetRowEntity> convertDatas = importRequest.getConvertResultList();

        return new ResultInfo.Builder<>().success();

    }

    /**
     * 获取表头
     */
    @GetMapping(value = "/headers")
    @FunctionDefinition(functionName = "通用导入获取表头demo", functionCode = ModuleCodeStore.Function.VIEW, isPublic = true)
    public ImportHeaderResult getImportHeader() {
        ImportHeaderResult result = new ImportHeaderResult();

        List<ImportHeaderGroup> groups = Lists.newArrayList();
        ImportHeaderGroup group1 = new ImportHeaderGroup();
        ImportHeaderGroup group2 = new ImportHeaderGroup();
        group1.setCode("groupCode1");
        group1.setName("分组1");
        group2.setCode("groupCode2");
        group2.setName("分组2");

        List<ImportHeaderItem> items = Lists.newArrayList();
        ImportHeaderItem item1 = new ImportHeaderItem();
        item1.setName("姓名");
        item1.setCode("name");
        item1.setSelected(false);
        ImportHeaderItem item2 = new ImportHeaderItem();

        item2.setName("手机号");
        item2.setCode("mobile");
        item2.setSelected(false);
        items.add(item1);
        items.add(item2);
        group1.setHeaderItems(items);


        List<ImportHeaderItem> items2 = Lists.newArrayList();
        ImportHeaderItem item3 = new ImportHeaderItem();
        item3.setName("年龄");
        item3.setCode("age");
        item3.setSelected(false);
        ImportHeaderItem item4 = new ImportHeaderItem();

        item4.setName("性别");
        item4.setCode("gender");
        item4.setSelected(false);
        items2.add(item3);
        items2.add(item4);


        group2.setHeaderItems(items2);

        groups.add(group1);
        groups.add(group2);

        result.setGroups(groups);
        return result;
    }

}
